// pages/mine/mobile/index.js
// var app = getApp()
// var Common = require('../../../utils/Common.js')
// var util = require('../../../utils/util.js')
// var C = require('../../../utils/config.js')
var req = require('../../interfaceImpl.js');
var config = require('../../interfaceConfig.js');
var app = getApp();
var clickpoint = 0;
Page({
  data: {
    tel: '',
    code: '',
    btntxt: '获取验证码',
    sendflag: false,
    second: 60,

  },
  onLoad: function (options) {
    //TODO 获取会员卡列表
    var that = this;
    this.setData({ hasReq: false })
    wx.setNavigationBarTitle({
      title: '绑定会员卡',
    })
  },
  getCode: function () {
    var that = this;
    if (this.validatemobile()) {
      // 设置为已经发送

      // 开始计时

      // 发起请求/获取验证码
      var phone = this.data.tel;
      return req.SendVipBindSms(phone, app.globalData.token).then(res => {
        console.log(res)
        if (res.data.success) {
          that.countdown();
          that.setData({
            sendflag: true,
          });
        }else{
          wx.showToast({
            title: '发送失败',
            image: '/static/images/cuo.png',
            duration: 1500
          })
        }
      })
    } else {
      return;
    }
  }, 
  // 保存手机号
  tobind: function () {
    clickpoint++;
    if (clickpoint >= 5) {
      wx.showToast({
        title: '点击过于频繁',
        image: '/static/images/cuo.png',
        duration: 1500,
        success: function () {
          wx.reLaunch({
            url: '../member/index',
          })
        }
      })
    } else {
      var that = this;
      var tel = that.data.tel
      var code_temp = that.data.code;
      var cardcode = that.data.cardcode
      console.log(code_temp)
      if (this.validatemobile() && code_temp.length !== 0 && cardcode !==''&&typeof(cardcode)!==undefined) {
        wx.showLoading({
          title: '请稍候', mask: true
        })
        return req.vipBind(cardcode, tel, code_temp, app.globalData.token).then(res => {
          //TODO 绑定会员卡
          console.log(res)
          wx.hideLoading();
          if (res.data.success) {
            this.setData({ hasReq: true })
             wx.showModal({
                        //title: '提交成功',
                        content: "绑定成功",
                        showCancel: false,
                        success: function (res) {
                            wx.navigateBack({ changed: false });
                        }
                    })
          } else {
            wx.showModal({
              title: '绑定失败',
              content: res.data.data,
              showCancel: false,
              success: function (res) {
              }
            })
          }
        })
      }
    }
  },
  // 验证手机号格式
  validatemobile: function () {
    var mobile = this.data.tel;
    if (mobile.length == 0) {
      wx.showToast({
        title: '请输入手机号！',
        image: '/static/images/cuo.png',
        duration: 1500
      })
      return false;
    }
    if (mobile.length != 11) {
      wx.showToast({
        title: '手机号长度有误！',
        image: '/static/images/cuo.png',
        duration: 1500
      })
      return false;
    }
    var myreg = /^(((13[0-9]{1})|(15[0-9]{1})|(18[0-9]{1})|(17[0-9]{1}))+\d{8})$/;
    if (!myreg.test(mobile)) {
      wx.showToast({
        title: '手机号格式错误！',
        image: '/static/images/cuo.png',
        duration: 1500
      })
      return false;
    }
    return true;
  },
  cardinput: function (e) {
    this.setData({
      cardcode: e.detail.value
    })
  },
  // 计时
  countdown: function () {
    var that = this;
    var second = that.data.second;
    if (second == 0) {
      // console.log("Time Out...");
      that.setData({
        sendflag: false,
        second: 60,
      });
      return;
    }
    var time = setTimeout(function () {
      that.setData({
        second: second - 1
      });
      that.countdown(that);
    }, 1000)
  },
  // 获取输入的手机号
  TelInput: function (e) {
    this.setData({
      tel: e.detail.value
    })
  },
  // 获取输入的验证码
  CodeInput: function (e) {
    this.setData({
      code: e.detail.value
    })
  },
  onShow: function () {
    clickpoint = 0;
  },
})